import express from 'express'
import useRes from '../hooks/useRes.js'
import path from 'path'
import fs from 'fs'

const router = express.Router()
const { setSuccess, seteError } = useRes()
const currentDir = process.cwd()

router.get('/:id', async (req, res, next) => {
  try {
    const { id } = req.params
    const filePath = path.join(currentDir, `./game/${id}.json`)
    const rawData = fs.readFileSync(filePath, 'utf8')
    res.json(setSuccess(JSON.parse(rawData)))
  } catch (e) {
    res.json(seteError(e.message))
  }
})

export default router
